package com.test.springboot.controller;

import com.test.springboot.domain.Stock;
import com.test.springboot.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

@RestController
public class StockController {
    @Autowired
    private StockService stockService;

    @GetMapping("/v2/dedect")
    public String deduct() {
        this.stockService.deduct();
        return "ok lyt";
    }

    @GetMapping("/v3/fairLock/{id}")
    public String fairLock(@PathVariable Long id) {
        this.stockService.testFairLock(id);
        return "hello lyt";
    }

    @GetMapping("/v3/readLock")
    public String readLock() {
        this.stockService.testReadLock();
        return "hello read";
    }

    @GetMapping("/v3/writerLock")
    public String writerLock() {
        this.stockService.testWriterLock();
        return "hello read";
    }

    @GetMapping("/v3/rsemaphore")
    public String testRsemaphore() {
        stockService.testRsemaphore();
        return "hell rsemaphore";
    }

    @GetMapping("/v3/semaphore")
    public String testSemaphorre() {
        this.stockService.testSemaphore();
        return "hello semaphore";
    }
    @GetMapping("/v3/latch")
    public String testLatch() {
        this.stockService.testLatch();
            return "班长锁门";

    }

    @GetMapping("/v3/countDown")
    public String testCountDown() {
        this.stockService.testCountDown();
            return "一位同学出门";

    }

    public static void main(String[] args) {
//        Semaphore semaphore = new Semaphore(3);
//        for (int i = 1; i <= 6; i++) {
//            new Thread(() -> {
//                try {
//                    semaphore.acquire();
//                    System.out.println(Thread.currentThread().getName() + "入场");
////                    Thread.sleep(5000);
//                    TimeUnit.SECONDS.sleep(new Random().nextInt(10));
//                    semaphore.release();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName() + "出场");
//
//            },i+"号车"
//            ).start();
//        }

        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <=6 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"准备出门。。。");
                try {
                    TimeUnit.SECONDS.sleep(new Random().nextInt(5));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName()+"已经出门");
            },i+"号同学").start();
        }
        try {
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName()+"班长锁门");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
